
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/gotchas.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:01 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Gotchas and Pitfalls &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="_static/default.css" />
    <link rel="stylesheet" type="text/css" href="_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
    <script src="_static/jquery.js"></script>
    <script src="_static/underscore.js"></script>
    <script src="_static/doctools.js"></script>
    <script src="../../live.sympy.org/static/utilities.js"></script>
    <script src="../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../live.sympy.org/static/live-core.js"></script>
    <script src="../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="_static/sympy-notailtext-favicon.ico"/>
    <link href="gotchas.html" rel="canonical" />
    
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="SymPy Modules Reference" href="modules/index.html" />
    <link rel="prev" title="Symbolic and fuzzy booleans" href="guides/booleans.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="modules/index.html" title="SymPy Modules Reference"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="guides/booleans.html" title="Symbolic and fuzzy booleans"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">SymPy 1.9 documentation</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Gotchas and Pitfalls</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="gotchas-and-pitfalls">
<span id="gotchas"></span><h1>Gotchas and Pitfalls<a class="headerlink" href="#gotchas-and-pitfalls" title="Permalink to this headline">¶</a></h1>
<section id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>SymPy runs under the <a class="reference external" href="https://www.python.org/">Python Programming Language</a>, so there are some things that may behave
differently than they do in other, independent computer algebra systems
like Maple or Mathematica.  These are some of the gotchas and pitfalls
that you may encounter when using SymPy.  See also the <a class="reference external" href="https://github.com/sympy/sympy/wiki/Faq">FAQ</a>, the <a class="reference internal" href="tutorial/index.html#tutorial"><span class="std std-ref">Tutorial</span></a>, the
remainder of the SymPy Docs, and the <a class="reference external" href="https://docs.python.org/3/tutorial/">official Python Tutorial</a>.</p>
<p>If you are already familiar with C or Java, you might also want to look
at this <a class="reference external" href="https://nerdparadise.com/programming/python4minutes/">4 minute Python tutorial</a>.</p>
<p>Ignore <code class="docutils literal notranslate"><span class="pre">#doctest:</span> <span class="pre">+SKIP</span></code> in the examples.  That has to do with
internal testing of the examples.</p>
</section>
<section id="equals-signs">
<span id="id1"></span><h2>Equals Signs (=)<a class="headerlink" href="#equals-signs" title="Permalink to this headline">¶</a></h2>
<section id="single-equals-sign">
<h3>Single Equals Sign<a class="headerlink" href="#single-equals-sign" title="Permalink to this headline">¶</a></h3>
<p>The equals sign (<code class="docutils literal notranslate"><span class="pre">=</span></code>) is the assignment operator, not equality.  If
you want to do <span class="math notranslate nohighlight">\(x = y\)</span>, use <code class="docutils literal notranslate"><span class="pre">Eq(x,</span> <span class="pre">y)</span></code> for equality.
Alternatively, all expressions are assumed to equal zero, so you can
just subtract one side and use <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">y</span></code>.</p>
<p>The proper use of the equals sign is to assign expressions to variables.</p>
<p>For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">x</span> <span class="o">-</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">x - y</span>
</pre></div>
</div>
</section>
<section id="double-equals-signs">
<h3>Double Equals Signs<a class="headerlink" href="#double-equals-signs" title="Permalink to this headline">¶</a></h3>
<p>Double equals signs (<code class="docutils literal notranslate"><span class="pre">==</span></code>) are used to test equality.  However, this
tests expressions exactly, not symbolically.  For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">==</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">==</span> <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="go">True</span>
</pre></div>
</div>
<p>If you want to test for symbolic equality, one way is to subtract one
expression from the other and run it through functions like
<a class="reference internal" href="modules/core.html#sympy.core.function.expand" title="sympy.core.function.expand"><code class="xref py py-func docutils literal notranslate"><span class="pre">expand()</span></code></a>, <a class="reference internal" href="modules/simplify/simplify.html#sympy.simplify.simplify.simplify" title="sympy.simplify.simplify.simplify"><code class="xref py py-func docutils literal notranslate"><span class="pre">simplify()</span></code></a>, and <a class="reference internal" href="modules/simplify/simplify.html#sympy.simplify.trigsimp.trigsimp" title="sympy.simplify.trigsimp.trigsimp"><code class="xref py py-func docutils literal notranslate"><span class="pre">trigsimp()</span></code></a> and see if the
equation reduces to 0.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">simplify</span><span class="p">,</span> <span class="n">cos</span><span class="p">,</span> <span class="n">sin</span><span class="p">,</span> <span class="n">expand</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">((</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">eq</span><span class="p">)</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand</span><span class="p">(</span><span class="n">eq</span><span class="p">,</span> <span class="n">trig</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">0</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See also <a class="reference external" href="https://github.com/sympy/sympy/wiki/Faq">Why does SymPy say that two equal expressions are unequal?</a> in the FAQ.</p>
</div>
</section>
</section>
<section id="variables">
<h2>Variables<a class="headerlink" href="#variables" title="Permalink to this headline">¶</a></h2>
<section id="variables-assignment-does-not-create-a-relation-between-expressions">
<h3>Variables Assignment does not Create a Relation Between Expressions<a class="headerlink" href="#variables-assignment-does-not-create-a-relation-between-expressions" title="Permalink to this headline">¶</a></h3>
<p>When you use <code class="docutils literal notranslate"><span class="pre">=</span></code> to do assignment, remember that in Python, as in most
programming languages, the variable does not change if you change the
value you assigned to it.  The equations you are typing use the values
present at the time of creation to “fill in” values, just like regular
Python definitions. They are not altered by changes made afterwards.
Consider the following:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">)</span>  <span class="c1"># Symbol, `a`, stored as variable &quot;a&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">1</span>        <span class="c1"># an expression involving `a` stored as variable &quot;b&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">a + 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="mi">4</span>            <span class="c1"># &quot;a&quot; now points to literal integer 4, not Symbol(&#39;a&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>          <span class="c1"># &quot;b&quot; is still pointing at the expression involving `a`</span>
<span class="go">a + 1</span>
</pre></div>
</div>
<p>Changing quantity <code class="docutils literal notranslate"><span class="pre">a</span></code> does not change <code class="docutils literal notranslate"><span class="pre">b</span></code>; you are not working
with a set of simultaneous equations. It might be helpful to remember
that the string that gets printed when you print a variable referring to
a SymPy object is the string that was given to it when it was created;
that string does not have to be the same as the variable that you assign
it to.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">var</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">var</span><span class="p">(</span><span class="s1">&#39;rate time short_life&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">r</span><span class="o">*</span><span class="n">t</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">rate*time</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="mi">80</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>        <span class="c1"># We haven&#39;t changed d, only r and t</span>
<span class="go">rate*time</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">r</span><span class="o">*</span><span class="n">t</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>        <span class="c1"># Now d is using the current values of r and t</span>
<span class="go">160</span>
</pre></div>
</div>
<p>If you need variables that have dependence on each other, you can define
functions.  Use the <code class="docutils literal notranslate"><span class="pre">def</span></code> operator.  Indent the body of the function.
See the Python docs for more information on defining functions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">var</span><span class="p">(</span><span class="s1">&#39;c d&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="go">c</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">d</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">ctimesd</span><span class="p">():</span>
<span class="gp">... </span>    <span class="sd">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="sd">    This function returns whatever c is times whatever d is.</span>
<span class="gp">... </span><span class="sd">    &quot;&quot;&quot;</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">c</span><span class="o">*</span><span class="n">d</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ctimesd</span><span class="p">()</span>
<span class="go">c*d</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ctimesd</span><span class="p">()</span>
<span class="go">2*d</span>
</pre></div>
</div>
<p>If you define a circular relationship, you will get a
<code class="docutils literal notranslate"><span class="pre">RuntimeError</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">a</span><span class="p">():</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">b</span><span class="p">()</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">b</span><span class="p">():</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">a</span><span class="p">()</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">()</span> 
<span class="gt">Traceback (most recent call last):</span>
<span class="gr">  File &quot;...&quot;, line ..., in ...</span>
<span class="gr">    compileflags, 1) in test.globs</span>
<span class="gr">  File &quot;&lt;...&gt;&quot;, line 1, in &lt;module&gt;</span>
<span class="gr">    a()</span>
<span class="gr">  File &quot;&lt;...&gt;&quot;, line 2, in a</span>
<span class="gr">    return b()</span>
<span class="gr">  File &quot;&lt;...&gt;&quot;, line 2, in b</span>
<span class="gr">    return a()</span>
<span class="gr">  File &quot;&lt;...&gt;&quot;, line 2, in a</span>
<span class="gr">    return b()</span>
<span class="gr">...</span>
<span class="gr">RuntimeError</span>: <span class="n">maximum recursion depth exceeded</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See also <a class="reference external" href="https://github.com/sympy/sympy/wiki/Faq">Why doesn’t changing one variable change another that depends on it?</a> in the FAQ.</p>
</div>
</section>
<section id="symbols">
<span id="id2"></span><h3>Symbols<a class="headerlink" href="#symbols" title="Permalink to this headline">¶</a></h3>
<p>Symbols are variables, and like all other variables, they need to be
assigned before you can use them.  For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sympy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span><span class="o">**</span><span class="mi">2</span>  <span class="c1"># z is not defined yet </span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">NameError</span>: <span class="n">name &#39;z&#39; is not defined</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sympy</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="s1">&#39;z&#39;</span><span class="p">)</span>  <span class="c1"># This is the easiest way to define z as a standard symbol</span>
<span class="go">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span><span class="o">**</span><span class="mi">2</span>
<span class="go">z**2</span>
</pre></div>
</div>
<p>If you use <strong class="command">isympy</strong>, it runs the following commands for you,
giving you some default Symbols and Functions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">t</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y z t&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">n</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;k m n&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">,</span> <span class="n">g</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;f g h&#39;</span><span class="p">,</span> <span class="bp">cls</span><span class="o">=</span><span class="n">Function</span><span class="p">)</span>
</pre></div>
</div>
<p>You can also import common symbol names from <a class="reference internal" href="modules/abc.html#module-sympy.abc" title="sympy.abc"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.abc</span></code></a>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">w</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span>
<span class="go">w</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sympy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">(</span><span class="n">sympy</span><span class="o">.</span><span class="n">abc</span><span class="p">)</span>  
<span class="go">[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;,</span>
<span class="go">&#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;Symbol&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;,</span>
<span class="go">&#39;__builtins__&#39;, &#39;__doc__&#39;, &#39;__file__&#39;, &#39;__name__&#39;, &#39;__package__&#39;, &#39;_greek&#39;,</span>
<span class="go">&#39;_latin&#39;, &#39;a&#39;, &#39;alpha&#39;, &#39;b&#39;, &#39;beta&#39;, &#39;c&#39;, &#39;chi&#39;, &#39;d&#39;, &#39;delta&#39;, &#39;e&#39;,</span>
<span class="go">&#39;epsilon&#39;, &#39;eta&#39;, &#39;f&#39;, &#39;g&#39;, &#39;gamma&#39;, &#39;h&#39;, &#39;i&#39;, &#39;iota&#39;, &#39;j&#39;, &#39;k&#39;, &#39;kappa&#39;,</span>
<span class="go">&#39;l&#39;, &#39;m&#39;, &#39;mu&#39;, &#39;n&#39;, &#39;nu&#39;, &#39;o&#39;, &#39;omega&#39;, &#39;omicron&#39;, &#39;p&#39;, &#39;phi&#39;, &#39;pi&#39;,</span>
<span class="go">&#39;psi&#39;, &#39;q&#39;, &#39;r&#39;, &#39;rho&#39;, &#39;s&#39;, &#39;sigma&#39;, &#39;t&#39;, &#39;tau&#39;, &#39;theta&#39;, &#39;u&#39;, &#39;upsilon&#39;,</span>
<span class="go">&#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;xi&#39;, &#39;y&#39;, &#39;z&#39;, &#39;zeta&#39;]</span>
</pre></div>
</div>
<p>If you want control over the assumptions of the variables, use
<a class="reference internal" href="modules/core.html#sympy.core.symbol.Symbol" title="sympy.core.symbol.Symbol"><code class="xref py py-class docutils literal notranslate"><span class="pre">Symbol</span></code></a> and <a class="reference internal" href="modules/core.html#sympy.core.symbol.symbols" title="sympy.core.symbol.symbols"><code class="xref py py-func docutils literal notranslate"><span class="pre">symbols()</span></code></a>.  See <a class="reference internal" href="#keyword-arguments"><span class="std std-ref">Keyword
Arguments</span></a> below.</p>
<p>Lastly, it is recommended that you not use <a class="reference internal" href="modules/core.html#sympy.core.numbers.ImaginaryUnit" title="sympy.core.numbers.ImaginaryUnit"><code class="xref py py-obj docutils literal notranslate"><span class="pre">I</span></code></a>, <a class="reference internal" href="modules/stats.html#sympy.stats.E" title="sympy.stats.E"><code class="xref py py-obj docutils literal notranslate"><span class="pre">E</span></code></a>, <a class="reference internal" href="modules/physics/quantum/gate.html#sympy.physics.quantum.gate.S" title="sympy.physics.quantum.gate.S"><code class="xref py py-obj docutils literal notranslate"><span class="pre">S</span></code></a>, <a class="reference internal" href="modules/core.html#sympy.core.evalf.N" title="sympy.core.evalf.N"><code class="xref py py-obj docutils literal notranslate"><span class="pre">N</span></code></a>, <code class="docutils literal notranslate"><span class="pre">C</span></code>, <a class="reference internal" href="modules/series/series.html#sympy.series.order.Order" title="sympy.series.order.Order"><code class="xref py py-obj docutils literal notranslate"><span class="pre">O</span></code></a>, or <a class="reference internal" href="modules/assumptions/ask.html#sympy.assumptions.ask.AssumptionKeys" title="sympy.assumptions.ask.AssumptionKeys"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Q</span></code></a> for variable or symbol names, as those
are used for the imaginary unit (<span class="math notranslate nohighlight">\(i\)</span>), the base of the natural logarithm
(<span class="math notranslate nohighlight">\(e\)</span>), the <a class="reference internal" href="modules/core.html#sympy.core.sympify.sympify" title="sympy.core.sympify.sympify"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympify()</span></code></a> function (see <a class="reference internal" href="#symbolic-expressions"><span class="std std-ref">Symbolic
Expressions</span></a> below), numeric evaluation (<a class="reference internal" href="modules/core.html#sympy.core.evalf.N" title="sympy.core.evalf.N"><code class="xref py py-func docutils literal notranslate"><span class="pre">N()</span></code></a> is
equivalent to <a class="reference internal" href="modules/evalf.html#evalf-label"><span class="std std-ref">evalf()</span></a> ), the <a class="reference external" href="https://en.wikipedia.org/wiki/Big_O_notation">big O</a> order symbol (as in
<span class="math notranslate nohighlight">\(O(n\log{n})\)</span>), and the assumptions object that holds a list of
supported ask keys (such as <code class="docutils literal notranslate"><span class="pre">Q.real</span></code>), respectively. You can use the
mnemonic <code class="docutils literal notranslate"><span class="pre">OSINEQ</span></code> to remember what Symbols are defined by default in SymPy.
Or better yet, always use lowercase letters for Symbol names. Python will not
prevent you from overriding default SymPy names or functions, so be careful.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cos</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span>  <span class="c1"># cos and pi are a built-in sympy names.</span>
<span class="go">-1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pi</span> <span class="o">=</span> <span class="mi">3</span>   <span class="c1"># Notice that there is no warning for overriding pi.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cos</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span>
<span class="go">cos(3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">cos</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>  <span class="c1"># No warning for overriding built-in functions either.</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="mi">5</span><span class="o">*</span><span class="n">x</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cos</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span>
<span class="go">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">cos</span>  <span class="c1"># reimport to restore normal behavior</span>
</pre></div>
</div>
<p>To get a full list of all default names in SymPy do:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sympy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">(</span><span class="n">sympy</span><span class="p">)</span>  
<span class="go"># A big list of all default sympy names and functions follows.</span>
<span class="go"># Ignore everything that starts and ends with __.</span>
</pre></div>
</div>
<p>If you have <a class="reference external" href="https://ipython.org/">IPython</a> installed and
use <strong class="command">isympy</strong>, you can also press the TAB key to get a list of
all built-in names and to autocomplete.  Also, see <a class="reference external" href="https://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks">this page</a> for a
trick for getting tab completion in the regular Python console.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See also <a class="reference external" href="https://github.com/sympy/sympy/wiki/Faq">What is the best way to create symbols?</a> in the FAQ.</p>
</div>
</section>
<section id="functions">
<span id="calling-functions"></span><h3>Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h3>
<p>A function like <code class="docutils literal notranslate"><span class="pre">f(x)</span></code> can be created by defining the Function
and the variable:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">Function</span><span class="p">(</span><span class="s1">&#39;f&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">f(x)</span>
</pre></div>
</div>
<p>If you assign <code class="docutils literal notranslate"><span class="pre">f(x)</span></code> to a Python variable <span class="math notranslate nohighlight">\(f\)</span> you will lose your
ability to copy and paste that function or to create a function
with a different argument: <code class="docutils literal notranslate"><span class="pre">Function('f')</span></code> is callable, but
<code class="docutils literal notranslate"><span class="pre">Function('f')(x)</span></code> is not:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f1</span> <span class="o">=</span> <span class="n">Function</span><span class="p">(</span><span class="s1">&#39;f1&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f2</span> <span class="o">=</span> <span class="n">Function</span><span class="p">(</span><span class="s1">&#39;f2&#39;</span><span class="p">)(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f1</span>
<span class="go">f1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f2</span>
<span class="go">f2(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f1</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">f1(1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f2</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">&#39;f2&#39; object is not callable</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f2</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">f2(1)</span>
</pre></div>
</div>
</section>
</section>
<section id="symbolic-expressions">
<span id="id3"></span><h2>Symbolic Expressions<a class="headerlink" href="#symbolic-expressions" title="Permalink to this headline">¶</a></h2>
<section id="python-numbers-vs-sympy-numbers">
<span id="python-vs-sympy-numbers"></span><h3>Python numbers vs. SymPy Numbers<a class="headerlink" href="#python-numbers-vs-sympy-numbers" title="Permalink to this headline">¶</a></h3>
<p>SymPy uses its own classes for integers, rational numbers, and floating
point numbers instead of the default Python <code class="docutils literal notranslate"><span class="pre">int</span></code> and <code class="docutils literal notranslate"><span class="pre">float</span></code>
types because it allows for more control.  But you have to be careful.
If you type an expression that just has numbers in it, it will default
to a Python expression.  Use the <a class="reference internal" href="modules/core.html#sympy.core.sympify.sympify" title="sympy.core.sympify.sympify"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympify()</span></code></a> function, or just
<a class="reference internal" href="modules/physics/quantum/gate.html#sympy.physics.quantum.gate.S" title="sympy.physics.quantum.gate.S"><code class="xref py py-obj docutils literal notranslate"><span class="pre">S</span></code></a>, to ensure that something is a SymPy expression.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mf">6.2</span>  <span class="c1"># Python float. Notice the floating point accuracy problems.</span>
<span class="go">6.2000000000000002</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="mf">6.2</span><span class="p">)</span>  <span class="c1"># &lt;type &#39;float&#39;&gt; in Python 2.x,  &lt;class &#39;float&#39;&gt; in Py3k</span>
<span class="go">&lt;... &#39;float&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span><span class="p">(</span><span class="mf">6.2</span><span class="p">)</span>  <span class="c1"># SymPy Float has no such problems because of arbitrary precision.</span>
<span class="go">6.20000000000000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="mf">6.2</span><span class="p">))</span>
<span class="go">&lt;class &#39;sympy.core.numbers.Float&#39;&gt;</span>
</pre></div>
</div>
<p>If you include numbers in a SymPy expression, they will be sympified
automatically, but there is one gotcha you should be aware of.  If you
do <code class="docutils literal notranslate"><span class="pre">&lt;number&gt;/&lt;number&gt;</span></code> inside of a SymPy expression, Python will
evaluate the two numbers before SymPy has a chance to get
to them.  The solution is to <a class="reference internal" href="modules/core.html#sympy.core.sympify.sympify" title="sympy.core.sympify.sympify"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympify()</span></code></a> one of the numbers, or use
<a class="reference internal" href="modules/core.html#sympy.core.numbers.Rational" title="sympy.core.numbers.Rational"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Rational</span></code></a>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span>  <span class="c1"># evaluates to x**0 or x**0.5</span>
<span class="go">x**0.5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span>  <span class="c1"># sympyify one of the ints</span>
<span class="go">sqrt(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">**</span><span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>  <span class="c1"># use the Rational class</span>
<span class="go">sqrt(x)</span>
</pre></div>
</div>
<p>With a power of <code class="docutils literal notranslate"><span class="pre">1/2</span></code> you can also use <code class="docutils literal notranslate"><span class="pre">sqrt</span></code> shorthand:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">==</span> <span class="n">x</span><span class="o">**</span><span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>If the two integers are not directly separated by a division sign then
you don’t have to worry about this problem:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">/</span><span class="mi">3</span><span class="p">)</span>
<span class="go">x**(2*x/3)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>A common mistake is copying an expression that is printed and
reusing it.  If the expression has a <a class="reference internal" href="modules/core.html#sympy.core.numbers.Rational" title="sympy.core.numbers.Rational"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Rational</span></code></a> (i.e.,
<code class="docutils literal notranslate"><span class="pre">&lt;number&gt;/&lt;number&gt;</span></code>) in it, you will not get the same result,
obtaining the Python result for the division rather than a SymPy
Rational.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">solve</span><span class="p">(</span><span class="mi">7</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span><span class="mi">22</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span>
<span class="go">[22/7]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">22</span><span class="o">/</span><span class="mi">7</span>  <span class="c1"># If we just copy and paste we get int 3 or a float</span>
<span class="go">3.142857142857143</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># One solution is to just assign the expression to a variable</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># if we need to use it again.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">solve</span><span class="p">(</span><span class="mi">7</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">22</span><span class="p">,</span> <span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">22/7</span>
</pre></div>
</div>
<p>The other solution is to put quotes around the expression
and run it through S() (i.e., sympify it):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">S</span><span class="p">(</span><span class="s2">&quot;22/7&quot;</span><span class="p">)</span>
<span class="go">22/7</span>
</pre></div>
</div>
</div>
<p>Also, if you do not use <strong class="command">isympy</strong>, you could use <code class="docutils literal notranslate"><span class="pre">from</span>
<span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">division</span></code> to prevent the <code class="docutils literal notranslate"><span class="pre">/</span></code> sign from performing
<a class="reference external" href="https://en.wikipedia.org/wiki/Integer_division">integer division</a>.</p>
<blockquote>
<div><div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span>   <span class="c1"># With division imported it evaluates to a python float</span>
<span class="go">0.5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span><span class="o">//</span><span class="mi">2</span>  <span class="c1"># You can still achieve integer division with //</span>
<span class="go">0</span>
</pre></div>
</div>
<p>But be careful: you will now receive floats where you might have desired
a Rational:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span>
<span class="go">x**0.5</span>
</pre></div>
</div>
</div></blockquote>
<p><a class="reference internal" href="modules/core.html#sympy.core.numbers.Rational" title="sympy.core.numbers.Rational"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Rational</span></code></a> only works for number/number and is only meant for
rational numbers.  If you want a fraction with symbols or expressions in
it, just use <code class="docutils literal notranslate"><span class="pre">/</span></code>.  If you do number/expression or expression/number,
then the number will automatically be converted into a SymPy Number.
You only need to be careful with number/number.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Rational</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">invalid input: x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">2</span><span class="o">/</span><span class="n">x</span>
<span class="go">2/x</span>
</pre></div>
</div>
</section>
<section id="evaluating-expressions-with-floats-and-rationals">
<h3>Evaluating Expressions with Floats and Rationals<a class="headerlink" href="#evaluating-expressions-with-floats-and-rationals" title="Permalink to this headline">¶</a></h3>
<p>SymPy keeps track of the precision of <code class="docutils literal notranslate"><span class="pre">Float</span></code> objects. The default precision is
15 digits. When an expression involving a <code class="docutils literal notranslate"><span class="pre">Float</span></code> is evaluated, the result
will be expressed to 15 digits of precision but those digits (depending
on the numbers involved with the calculation) may not all be significant.</p>
<p>The first issue to keep in mind is how the <code class="docutils literal notranslate"><span class="pre">Float</span></code> is created: it is created
with a value and a precision. The precision indicates how precise of a value
to use when that <code class="docutils literal notranslate"><span class="pre">Float</span></code> (or an expression it appears in) is evaluated.</p>
<p>The values can be given as strings, integers, floats, or rationals.</p>
<blockquote>
<div><ul class="simple">
<li><p>strings and integers are interpreted as exact</p></li>
</ul>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
<span class="go">100.000000000000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="s1">&#39;100&#39;</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">100.00</span>
</pre></div>
</div>
<ul class="simple">
<li><p>to have the precision match the number of digits, the null string
can be used for the precision</p></li>
</ul>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="go">100.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="s1">&#39;12.34&#39;</span><span class="p">)</span>
<span class="go">12.3400000000000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="s1">&#39;12.34&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="go">12.34</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="p">[</span><span class="n">Float</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;0.25&#39;</span><span class="p">,</span> <span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">7</span><span class="p">))]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">[</span><span class="n">s</span><span class="p">,</span> <span class="n">r</span><span class="p">]:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">0.250</span>
<span class="go">0.143</span>
</pre></div>
</div>
</div></blockquote>
<p>Next, notice that each of those values looks correct to 3 digits. But if we try
to evaluate them to 20 digits, a difference will become apparent:</p>
<blockquote>
<div><p>The 0.25 (with precision of 3) represents a number that has a non-repeating
binary decimal; 1/7 is repeating in binary and decimal – it cannot be
represented accurately too far past those first 3 digits (the correct
decimal is a repeating 142857):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">0.25000000000000000000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">0.14285278320312500000</span>
</pre></div>
</div>
<p>It is important to realize that although a Float is being displayed in
decimal at arbitrary precision, it is actually stored in binary. Once the
Float is created, its binary information is set at the given precision.
The accuracy of that value cannot be subsequently changed; so 1/7, at a
precision of 3 digits, can be padded with binary zeros, but these will
not make it a more accurate value of 1/7.</p>
</div></blockquote>
<p>If inexact, low-precision numbers are involved in a calculation with
higher precision values, the evalf engine will increase the precision
of the low precision values and inexact results will be obtained. This is
feature of calculations with limited precision:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="s1">&#39;0.1&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span> <span class="o">+</span> <span class="n">Float</span><span class="p">(</span><span class="s1">&#39;0.1&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">0.2000061035</span>
</pre></div>
</div>
<p>Although the <code class="docutils literal notranslate"><span class="pre">evalf</span></code> engine tried to maintain 10 digits of precision (since
that was the highest precision represented) the 3-digit precision used
limits the accuracy to about 4 digits – not all the digits you see
are significant. evalf doesn’t try to keep track of the number of
significant digits.</p>
<p>That very simple expression involving the addition of two numbers with
different precisions will hopefully be instructive in helping you
understand why more complicated expressions (like trig expressions that
may not be simplified) will not evaluate to an exact zero even though,
with the right simplification, they should be zero. Consider this
unsimplified trig identity, multiplied by a big number:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">big</span> <span class="o">=</span> <span class="mi">12345678901234567890</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">big_trig_identity</span> <span class="o">=</span> <span class="n">big</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">big</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">big</span><span class="o">*</span><span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">abs</span><span class="p">(</span><span class="n">big_trig_identity</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mf">.1</span><span class="p">)</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span> <span class="o">&gt;</span> <span class="mi">1000</span>
<span class="go">True</span>
</pre></div>
</div>
<p>When the <span class="math notranslate nohighlight">\(\cos\)</span> and <span class="math notranslate nohighlight">\(\sin\)</span> terms were evaluated to 15 digits of precision and
multiplied by the big number, they gave a large number that was only
precise to 15 digits (approximately) and when the 20 digit big number
was subtracted the result was not zero.</p>
<p>There are three things that will help you obtain more precise numerical
values for expressions:</p>
<blockquote>
<div><p>1) Pass the desired substitutions with the call to evaluate. By doing
the subs first, the <code class="docutils literal notranslate"><span class="pre">Float</span></code> values cannot be updated as necessary. By
passing the desired substitutions with the call to evalf the ability
to re-evaluate as necessary is gained and the results are impressively
better:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">big_trig_identity</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="mf">0.1</span><span class="p">})</span>
<span class="go">-0.e-91</span>
</pre></div>
</div>
<p>2) Use Rationals, not Floats. During the evaluation process, the
Rational can be computed to an arbitrary precision while the Float,
once created – at a default of 15 digits – cannot. Compare the
value of <code class="docutils literal notranslate"><span class="pre">-1.4e+3</span></code> above with the nearly zero value obtained when
replacing x with a Rational representing 1/10 – before the call
to evaluate:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">big_trig_identity</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">S</span><span class="p">(</span><span class="s1">&#39;1/10&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">0.e-91</span>
</pre></div>
</div>
<p>3) Try to simplify the expression. In this case, SymPy will recognize
the trig identity and simplify it to zero so you don’t even have to
evaluate it numerically:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">big_trig_identity</span><span class="o">.</span><span class="n">simplify</span><span class="p">()</span>
<span class="go">0</span>
</pre></div>
</div>
</div></blockquote>
</section>
<section id="immutability-of-expressions">
<span id="id4"></span><h3>Immutability of Expressions<a class="headerlink" href="#immutability-of-expressions" title="Permalink to this headline">¶</a></h3>
<p>Expressions in SymPy are immutable, and cannot be modified by an in-place
operation.  This means that a function will always return an object, and the
original expression will not be modified. The following example snippet
demonstrates how this works:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="n">var</span><span class="p">(</span><span class="s1">&#39;x y a b&#39;</span><span class="p">)</span>
    <span class="n">expr</span> <span class="o">=</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;original =&#39;</span><span class="p">,</span> <span class="n">expr</span><span class="p">)</span>
    <span class="n">expr_modified</span> <span class="o">=</span> <span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">({</span><span class="n">x</span><span class="p">:</span> <span class="n">a</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">b</span><span class="p">})</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;modified =&#39;</span><span class="p">,</span> <span class="n">expr_modified</span><span class="p">)</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>The output shows that the <a class="reference internal" href="modules/core.html#sympy.core.basic.Basic.subs" title="sympy.core.basic.Basic.subs"><code class="xref py py-obj docutils literal notranslate"><span class="pre">subs()</span></code></a> function has replaced variable
<code class="docutils literal notranslate"><span class="pre">x</span></code> with variable <code class="docutils literal notranslate"><span class="pre">a</span></code>, and variable <code class="docutils literal notranslate"><span class="pre">y</span></code> with variable <code class="docutils literal notranslate"><span class="pre">b</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">original</span> <span class="o">=</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span>
<span class="n">modified</span> <span class="o">=</span> <span class="mi">3</span><span class="o">*</span><span class="n">a</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">b</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="modules/core.html#sympy.core.basic.Basic.subs" title="sympy.core.basic.Basic.subs"><code class="xref py py-obj docutils literal notranslate"><span class="pre">subs()</span></code></a> function does not modify the original expression <code class="docutils literal notranslate"><span class="pre">expr</span></code>.
Rather, a modified copy of the expression is returned. This returned object
is stored in the variable <code class="docutils literal notranslate"><span class="pre">expr_modified</span></code>. Note that unlike C/C++ and
other high-level languages, Python does not require you to declare a variable
before it is used.</p>
</section>
<section id="mathematical-operators">
<h3>Mathematical Operators<a class="headerlink" href="#mathematical-operators" title="Permalink to this headline">¶</a></h3>
<p>SymPy uses the same default operators as Python.  Most of these, like
<code class="docutils literal notranslate"><span class="pre">*/+-</span></code>, are standard.  Aside from integer division discussed in
<a class="reference internal" href="#python-vs-sympy-numbers"><span class="std std-ref">Python numbers vs. SymPy Numbers</span></a> above,
you should also be aware that implied multiplication is not allowed. You
need to use <code class="docutils literal notranslate"><span class="pre">*</span></code> whenever you wish to multiply something.  Also, to
raise something to a power, use <code class="docutils literal notranslate"><span class="pre">**</span></code>, not <code class="docutils literal notranslate"><span class="pre">^</span></code> as many computer
algebra systems use.  Parentheses <code class="docutils literal notranslate"><span class="pre">()</span></code> change operator precedence as
you would normally expect.</p>
<p>In <strong class="command">isympy</strong>, with the <strong class="command">ipython</strong> shell:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mi">2</span><span class="n">x</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">SyntaxError</span>: <span class="n">invalid syntax</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">2</span><span class="o">*</span><span class="n">x</span>
<span class="go">2*x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">^</span><span class="mi">2</span>  <span class="c1"># This is not power.  Use ** instead.</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">unsupported operand type(s) for ^: &#39;Add&#39; and &#39;int&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="go">(x + 1)**2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="mi">3</span> <span class="o">-</span> <span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
<span class="go">    2*x</span>
<span class="go">   x</span>
<span class="go">- ----- + 3</span>
<span class="go">  x + 1</span>
</pre></div>
</div>
</section>
<section id="inverse-trig-functions">
<h3>Inverse Trig Functions<a class="headerlink" href="#inverse-trig-functions" title="Permalink to this headline">¶</a></h3>
<p>SymPy uses different names for some functions than most computer algebra
systems.  In particular, the inverse trig functions use the python names
of <a class="reference internal" href="modules/functions/elementary.html#sympy.functions.elementary.trigonometric.asin" title="sympy.functions.elementary.trigonometric.asin"><code class="xref py py-obj docutils literal notranslate"><span class="pre">asin</span></code></a>, <a class="reference internal" href="modules/functions/elementary.html#sympy.functions.elementary.trigonometric.acos" title="sympy.functions.elementary.trigonometric.acos"><code class="xref py py-obj docutils literal notranslate"><span class="pre">acos</span></code></a> and so on instead of the usual <code class="docutils literal notranslate"><span class="pre">arcsin</span></code>
and <code class="docutils literal notranslate"><span class="pre">arccos</span></code>.  Use the methods described in <a class="reference internal" href="#symbols"><span class="std std-ref">Symbols</span></a>
above to see the names of all SymPy functions.</p>
</section>
<section id="sqrt-is-not-a-function">
<h3>Sqrt is not a Function<a class="headerlink" href="#sqrt-is-not-a-function" title="Permalink to this headline">¶</a></h3>
<p>There is no <code class="docutils literal notranslate"><span class="pre">sqrt</span></code> function in the same way that there is an
exponential function (<code class="docutils literal notranslate"><span class="pre">exp</span></code>). <code class="docutils literal notranslate"><span class="pre">sqrt(x)</span></code> is used to represent
<code class="docutils literal notranslate"><span class="pre">Pow(x,</span> <span class="pre">S(1)/2)</span></code> so if you want to know if an expression has any
square roots in it, <code class="docutils literal notranslate"><span class="pre">expr.has(sqrt)</span></code> will not work. You must look
for <code class="docutils literal notranslate"><span class="pre">Pow</span></code> with an exponent of one half (or negative one half if it
is in a denominator, e.g.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">Wild</span><span class="p">(</span><span class="s1">&#39;w&#39;</span><span class="p">)</span><span class="o">**</span><span class="n">S</span><span class="o">.</span><span class="n">Half</span><span class="p">)</span>
<span class="go">{sqrt(x)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="mi">1</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">Wild</span><span class="p">(</span><span class="s1">&#39;w&#39;</span><span class="p">)</span><span class="o">**-</span><span class="n">S</span><span class="o">.</span><span class="n">Half</span><span class="p">)</span>
<span class="go">{1/sqrt(x)}</span>
</pre></div>
</div>
<p>If you are interested in any power of the <code class="docutils literal notranslate"><span class="pre">sqrt</span></code> then the
following pattern would be appropriate</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sq</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">is_Pow</span> <span class="ow">and</span> <span class="n">s</span><span class="o">.</span><span class="n">exp</span><span class="o">.</span><span class="n">is_Rational</span> <span class="ow">and</span> <span class="n">s</span><span class="o">.</span><span class="n">exp</span><span class="o">.</span><span class="n">q</span> <span class="o">==</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">sq</span><span class="p">)</span>
<span class="go">{x**(3/2)}</span>
</pre></div>
</div>
</section>
</section>
<section id="special-symbols">
<h2>Special Symbols<a class="headerlink" href="#special-symbols" title="Permalink to this headline">¶</a></h2>
<p>The symbols <code class="docutils literal notranslate"><span class="pre">[]</span></code>, <code class="docutils literal notranslate"><span class="pre">{}</span></code>, <code class="docutils literal notranslate"><span class="pre">=</span></code>, and <code class="docutils literal notranslate"><span class="pre">()</span></code> have special meanings in
Python, and thus in SymPy.  See the Python docs linked to above for
additional information.</p>
<section id="lists">
<span id="id5"></span><h3>Lists<a class="headerlink" href="#lists" title="Permalink to this headline">¶</a></h3>
<p>Square brackets <code class="docutils literal notranslate"><span class="pre">[]</span></code> denote a list.  A list is a container that holds
any number of different objects.  A list can contain anything, including
items of different types.  Lists are mutable, which means that you can
change the elements of a list after it has been created.  You access the
items of a list also using square brackets, placing them after the list
or list variable.  Items are numbered using the space before the item.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>List indexes begin at 0.</p>
</div>
<p>Example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>  <span class="c1"># A simple list of two items</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[x, 1]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>  <span class="c1"># This is the first item</span>
<span class="go">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>  <span class="c1"># You can change values of lists after they have been created</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">[2, 1]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">solve</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span> <span class="c1"># Some functions return lists</span>
<span class="go">[-1 + sqrt(2), -sqrt(2) - 1]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See the Python docs for more information on lists and the square
bracket notation for accessing elements of a list.</p>
</div>
</section>
<section id="dictionaries">
<h3>Dictionaries<a class="headerlink" href="#dictionaries" title="Permalink to this headline">¶</a></h3>
<p>Curly brackets <code class="docutils literal notranslate"><span class="pre">{}</span></code> denote a dictionary, or a dict for short.  A
dictionary is an unordered list of non-duplicate keys and values.  The
syntax is <code class="docutils literal notranslate"><span class="pre">{key:</span> <span class="pre">value}</span></code>.  You can access values of keys using square
bracket notation.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>  <span class="c1"># A dictionary.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
<span class="go">{&#39;a&#39;: 1, &#39;b&#39;: 2}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">]</span>  <span class="c1"># How to access items in a dict</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">roots</span><span class="p">((</span><span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span>  <span class="c1"># Some functions return dicts</span>
<span class="go">{1: 2, 2: 1}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Some SymPy functions return dictionaries.  For example,</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># roots returns a dictionary of root:multiplicity items.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">roots</span><span class="p">((</span><span class="n">x</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span>
<span class="go">{-3: 1, 5: 2}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># This means that the root -3 occurs once and the root 5 occurs twice.</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See the Python docs for more information on dictionaries.</p>
</div>
</section>
<section id="tuples">
<h3>Tuples<a class="headerlink" href="#tuples" title="Permalink to this headline">¶</a></h3>
<p>Parentheses <code class="docutils literal notranslate"><span class="pre">()</span></code>, aside from changing operator precedence and their
use in function calls, (like <code class="docutils literal notranslate"><span class="pre">cos(x)</span></code>), are also used for tuples.  A
<code class="docutils literal notranslate"><span class="pre">tuple</span></code> is identical to a <a class="reference internal" href="#lists"><span class="std std-ref">list</span></a>, except that it is not
mutable.  That means that you cannot change their values after they
have been created.  In general, you will not need tuples in SymPy, but
sometimes it can be more convenient to type parentheses instead of
square brackets.</p>
<blockquote>
<div><div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>  <span class="c1"># Tuples are like lists</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span>
<span class="go">(1, 2, x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4</span>  <span class="c1"># Except you cannot change them after they have been created</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;console&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">TypeError</span>: <span class="n">&#39;tuple&#39; object does not support item assignment</span>
</pre></div>
</div>
<p>Single element tuples, unlike lists, must have a comma in them:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">x</span><span class="p">,)</span>
<span class="go">(x,)</span>
</pre></div>
</div>
<p>Without the comma, a single expression without a comma is not a tuple:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">x</span>
</pre></div>
</div>
<p>integrate takes a sequence as the second argument if you want to integrate
with limits (and a tuple or list will work):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">integrate</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="go">1/3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">integrate</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
<span class="go">1/3</span>
</pre></div>
</div>
</div></blockquote>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See the Python docs for more information on tuples.</p>
</div>
</section>
<section id="keyword-arguments">
<span id="id6"></span><h3>Keyword Arguments<a class="headerlink" href="#keyword-arguments" title="Permalink to this headline">¶</a></h3>
<p>Aside from the usage described <a class="reference internal" href="#equals-signs"><span class="std std-ref">above</span></a>, equals signs
(<code class="docutils literal notranslate"><span class="pre">=</span></code>) are also used to give named arguments to functions.  Any
function that has <code class="docutils literal notranslate"><span class="pre">key=value</span></code> in its parameters list (see below on how
to find this out), then <code class="docutils literal notranslate"><span class="pre">key</span></code> is set to <code class="docutils literal notranslate"><span class="pre">value</span></code> by default.  You can
change the value of the key by supplying your own value using the equals
sign in the function call.  Also, functions that have <code class="docutils literal notranslate"><span class="pre">**</span></code> followed by
a name in the parameters list (usually <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> or
<code class="docutils literal notranslate"><span class="pre">**assumptions</span></code>) allow you to add any number of <code class="docutils literal notranslate"><span class="pre">key=value</span></code> pairs
that you want, and they will all be evaluated according to the function.</p>
<blockquote>
<div><p><code class="docutils literal notranslate"><span class="pre">sqrt(x**2)</span></code> doesn’t auto simplify to x because x is assumed to be
complex by default, and, for example, <code class="docutils literal notranslate"><span class="pre">sqrt((-1)**2)</span> <span class="pre">==</span> <span class="pre">sqrt(1)</span> <span class="pre">==</span> <span class="pre">1</span> <span class="pre">!=</span> <span class="pre">-1</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">sqrt(x**2)</span>
</pre></div>
</div>
<p>Giving assumptions to Symbols is an example of using the keyword argument:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>The square root will now simplify since it knows that <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&gt;=</span> <span class="pre">0</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">x</span>
</pre></div>
</div>
<p>powsimp has a default argument of <code class="docutils literal notranslate"><span class="pre">combine='all'</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">m</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">m</span><span class="p">))</span>
<span class="go">     m + n</span>
<span class="go">(x*y)</span>
</pre></div>
</div>
<p>Setting combine to the default value is the same as not setting it.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">m</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">m</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;all&#39;</span><span class="p">))</span>
<span class="go">     m + n</span>
<span class="go">(x*y)</span>
</pre></div>
</div>
<p>The non-default options are <code class="docutils literal notranslate"><span class="pre">'exp'</span></code>, which combines exponents…</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">m</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">m</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;exp&#39;</span><span class="p">))</span>
<span class="go"> m + n  m + n</span>
<span class="go">x     *y</span>
</pre></div>
</div>
<p>…and ‘base’, which combines bases.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">m</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">m</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;base&#39;</span><span class="p">))</span>
<span class="go">     m      n</span>
<span class="go">(x*y) *(x*y)</span>
</pre></div>
</div>
</div></blockquote>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See the Python docs for more information on function parameters.</p>
</div>
</section>
</section>
<section id="getting-help-from-within-sympy">
<h2>Getting help from within SymPy<a class="headerlink" href="#getting-help-from-within-sympy" title="Permalink to this headline">¶</a></h2>
<section id="help">
<h3>help()<a class="headerlink" href="#help" title="Permalink to this headline">¶</a></h3>
<p>Although all docs are available at <a class="reference external" href="https://docs.sympy.org/">docs.sympy.org</a> or on the
<a class="reference external" href="https://wiki.sympy.org/">SymPy Wiki</a>, you can also get info on functions from within the
Python interpreter that runs SymPy.  The easiest way to do this is to do
<code class="docutils literal notranslate"><span class="pre">help(function)</span></code>, or <code class="docutils literal notranslate"><span class="pre">function?</span></code> if you are using <strong class="command">ipython</strong>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>In [1]: help(powsimp)  # help() works everywhere

In [2]: # But in ipython, you can also use ?, which is better because it
In [3]: # it gives you more information
In [4]: powsimp?
</pre></div>
</div>
<p>These will give you the function parameters and docstring for
<a class="reference internal" href="modules/simplify/simplify.html#sympy.simplify.powsimp.powsimp" title="sympy.simplify.powsimp.powsimp"><code class="xref py py-func docutils literal notranslate"><span class="pre">powsimp()</span></code></a>.  The output will look something like this:</p>
<span class="target" id="module-sympy.simplify.simplify"></span><dl class="py function">
<dt class="sig sig-object py">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">powsimp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">expr</span></em>, <em class="sig-param"><span class="pre">deep=False</span></em>, <em class="sig-param"><span class="pre">combine='all'</span></em>, <em class="sig-param"><span class="pre">force=False</span></em>, <em class="sig-param"><span class="pre">measure=&lt;function</span> <span class="pre">count_ops&gt;</span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/powsimp.py#L15-L484"><span class="viewcode-link"><span class="pre">[source]</span></span></a></dt>
<dd><p>reduces expression by combining powers with similar bases and exponents.</p>
<p class="rubric">Explanation</p>
<p>If <code class="docutils literal notranslate"><span class="pre">deep</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> then powsimp() will also simplify arguments of
functions. By default <code class="docutils literal notranslate"><span class="pre">deep</span></code> is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">force</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> then bases will be combined without checking for
assumptions, e.g. sqrt(x)*sqrt(y) -&gt; sqrt(x*y) which is not true
if x and y are both negative.</p>
<p>You can make powsimp() only combine bases or only combine exponents by
changing combine=’base’ or combine=’exp’.  By default, combine=’all’,
which does both.  combine=’base’ will only combine:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span>   <span class="n">a</span>          <span class="n">a</span>                          <span class="mi">2</span><span class="n">x</span>      <span class="n">x</span>
<span class="n">x</span> <span class="o">*</span> <span class="n">y</span>  <span class="o">=&gt;</span>  <span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span>   <span class="k">as</span> <span class="n">well</span> <span class="k">as</span> <span class="n">things</span> <span class="n">like</span> <span class="mi">2</span>   <span class="o">=&gt;</span>  <span class="mi">4</span>
</pre></div>
</div>
<p>and combine=’exp’ will only combine</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span>   <span class="n">b</span>      <span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span>
<span class="n">x</span> <span class="o">*</span> <span class="n">x</span>  <span class="o">=&gt;</span>  <span class="n">x</span>
</pre></div>
</div>
<p>combine=’exp’ will strictly only combine exponents in the way that used
to be automatic.  Also use deep=True if you need the old behavior.</p>
<p>When combine=’all’, ‘exp’ is evaluated first.  Consider the first
example below for when there could be an ambiguity relating to this.
This is done so things like the second example can be completely
combined.  If you want ‘base’ combined first, do something like
powsimp(powsimp(expr, combine=’base’), combine=’exp’).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">powsimp</span><span class="p">,</span> <span class="n">exp</span><span class="p">,</span> <span class="n">log</span><span class="p">,</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">n</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">z</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;all&#39;</span><span class="p">)</span>
<span class="go">x**(y + z)*y**z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">z</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;exp&#39;</span><span class="p">)</span>
<span class="go">x**(y + z)*y**z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">z</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;base&#39;</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">x**y*(x*y)**z</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;all&#39;</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(n*x)**(y + z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;exp&#39;</span><span class="p">)</span>
<span class="go">n**(y + z)*x**(y + z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;base&#39;</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(n*x)**y*(n*x)**z</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="n">y</span><span class="p">)))</span>
<span class="go">log(exp(x)*exp(y))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="n">y</span><span class="p">)),</span> <span class="n">deep</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">x + y</span>
</pre></div>
</div>
<p>Radicals with Mul bases will be combined if combine=’exp’</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Two radicals are automatically joined through Mul:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">**</span><span class="mi">3</span> <span class="o">==</span> <span class="n">a</span><span class="o">**</span><span class="mi">4</span>
<span class="go">True</span>
</pre></div>
</div>
<p>But if an integer power of that radical has been
autoexpanded then Mul does not join the resulting factors:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">**</span><span class="mi">4</span> <span class="c1"># auto expands to a Mul, no longer a Pow</span>
<span class="go">x**2*y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_</span><span class="o">*</span><span class="n">a</span> <span class="c1"># so Mul doesn&#39;t combine them</span>
<span class="go">x**2*y*sqrt(x*sqrt(y))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">_</span><span class="p">)</span> <span class="c1"># but powsimp will</span>
<span class="go">(x*sqrt(y))**(5/2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">a</span><span class="p">)</span> <span class="c1"># but won&#39;t when doing so would violate assumptions</span>
<span class="go">x*y*sqrt(x*sqrt(y))</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="source">
<h3>source()<a class="headerlink" href="#source" title="Permalink to this headline">¶</a></h3>
<p>Another useful option is the <a class="reference internal" href="modules/utilities/source.html#sympy.utilities.source.source" title="sympy.utilities.source.source"><code class="xref py py-func docutils literal notranslate"><span class="pre">source()</span></code></a> function.  This will print
the source code of a function, including any docstring that it may have.
You can also do <code class="docutils literal notranslate"><span class="pre">function??</span></code> in <strong class="command">ipython</strong>.  For example,
from SymPy 0.6.5:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">source</span><span class="p">(</span><span class="n">simplify</span><span class="p">)</span>  <span class="c1"># simplify() is actually only 2 lines of code. </span>
<span class="go">In file: ./sympy/simplify/simplify.py</span>
<span class="go">def simplify(expr):</span>
<span class="go">    &quot;&quot;&quot;Naively simplifies the given expression.</span>
<span class="go">       ...</span>
<span class="go">       Simplification is not a well defined term and the exact strategies</span>
<span class="go">       this function tries can change in the future versions of SymPy. If</span>
<span class="go">       your algorithm relies on &quot;simplification&quot; (whatever it is), try to</span>
<span class="go">       determine what you need exactly  -  is it powsimp()? radsimp()?</span>
<span class="go">       together()?, logcombine()?, or something else? And use this particular</span>
<span class="go">       function directly, because those are well defined and thus your algorithm</span>
<span class="go">       will be robust.</span>
<span class="go">       ...</span>
<span class="go">    &quot;&quot;&quot;</span>
<span class="go">    expr = Poly.cancel(powsimp(expr))</span>
<span class="go">    return powsimp(together(expr.expand()), combine=&#39;exp&#39;, deep=True)</span>
</pre></div>
</div>
</section>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Gotchas and Pitfalls</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#equals-signs">Equals Signs (=)</a><ul>
<li><a class="reference internal" href="#single-equals-sign">Single Equals Sign</a></li>
<li><a class="reference internal" href="#double-equals-signs">Double Equals Signs</a></li>
</ul>
</li>
<li><a class="reference internal" href="#variables">Variables</a><ul>
<li><a class="reference internal" href="#variables-assignment-does-not-create-a-relation-between-expressions">Variables Assignment does not Create a Relation Between Expressions</a></li>
<li><a class="reference internal" href="#symbols">Symbols</a></li>
<li><a class="reference internal" href="#functions">Functions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#symbolic-expressions">Symbolic Expressions</a><ul>
<li><a class="reference internal" href="#python-numbers-vs-sympy-numbers">Python numbers vs. SymPy Numbers</a></li>
<li><a class="reference internal" href="#evaluating-expressions-with-floats-and-rationals">Evaluating Expressions with Floats and Rationals</a></li>
<li><a class="reference internal" href="#immutability-of-expressions">Immutability of Expressions</a></li>
<li><a class="reference internal" href="#mathematical-operators">Mathematical Operators</a></li>
<li><a class="reference internal" href="#inverse-trig-functions">Inverse Trig Functions</a></li>
<li><a class="reference internal" href="#sqrt-is-not-a-function">Sqrt is not a Function</a></li>
</ul>
</li>
<li><a class="reference internal" href="#special-symbols">Special Symbols</a><ul>
<li><a class="reference internal" href="#lists">Lists</a></li>
<li><a class="reference internal" href="#dictionaries">Dictionaries</a></li>
<li><a class="reference internal" href="#tuples">Tuples</a></li>
<li><a class="reference internal" href="#keyword-arguments">Keyword Arguments</a></li>
</ul>
</li>
<li><a class="reference internal" href="#getting-help-from-within-sympy">Getting help from within SymPy</a><ul>
<li><a class="reference internal" href="#help">help()</a></li>
<li><a class="reference internal" href="#source">source()</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="guides/booleans.html"
                        title="previous chapter">Symbolic and fuzzy booleans</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="modules/index.html"
                        title="next chapter">SymPy Modules Reference</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/gotchas.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="modules/index.html" title="SymPy Modules Reference"
             >next</a> |</li>
        <li class="right" >
          <a href="guides/booleans.html" title="Symbolic and fuzzy booleans"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">SymPy 1.9 documentation</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Gotchas and Pitfalls</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/gotchas.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:02 GMT -->
</html>